/**
 * @file const.cpp
 * @author shaojiale
 * @brief
 * @version 0.1
 * @date 2021-11-29
 *
 * @copyright Copyright (c) 2021
 *
 */
#include <iostream>

/* const 修饰变量 */

/**
 *                      局部         |     全局
 * 初始化                必须               必须
 * 通过自身直接修改自身    不能               不能
 * 通过指针间接修改自身    可以               不能
 * 存储位置               栈                堆
 * 全局const变量修改会导致程序segement fault
 *
 */
// const int a1; error
int       a;
const int a1 = 10;
const int a2 = 10;
const int a3 = a1;
const int a4 = a;
int const a5 = a;

int *      p  = &a;
/* const 和指针 */
/// 这两种一样，const都是修饰int, same : const int (*p)
/// p可以修改，*p不能, 可以不进行初始化
const int *p1 = &a;
int const *p2 = &a2;
/// p不能修改，*p可以
/// const 修饰 p，必须要初始化,而且要使用int初始化
int *const p3 = &a;
/// const 修饰int 和 p
/// p不能修改，*p也不能，必须要初始化
int const *const p4 = &a;
const int *const p5 = &a1;

/* const 修饰函数 */

int main(int argc, char const *argv[])
{
    std::cout << "---a---" << std::endl;
    std::cout << "a  " << a << std::endl;
    std::cout << "a1 " << a1 << std::endl;
    std::cout << "a2 " << a2 << std::endl;
    std::cout << "a3 " << a3 << std::endl;
    std::cout << "a4 " << a4 << std::endl;
    std::cout << "a5 " << a5 << std::endl;
    std::cout << "------" << std::endl;

    std::cout << "---p---" << std::endl;
    std::cout << "p  " << *p << std::endl;
    std::cout << "p1 " << *p1 << std::endl;
    std::cout << "p2 " << *p2 << std::endl;
    std::cout << "p3 " << *p3 << std::endl;
    std::cout << "p4 " << *p4 << std::endl;
    std::cout << "p5 " << *p5 << std::endl;
    std::cout << "------" << std::endl;
    int       b = a2;
    const int c = a2;
    const int d = 9;
    // c = 10; err
    // d = 10;

    // /// 用指针修改const变量
    // int *p = (int *)&a2;
    // *p     = 20;
    // std::cout << *p << std::endl;

    // int *p2 = (int *)&c;
    // *p2     = 20;
    // std::cout << c << std::endl;
    // std::cout << *p2 << std::endl;

    // int *p3 = (int *)&d;
    // *p3     = 20;
    // std::cout << c << std::endl;
    // std::cout << *p3 << std::endl;

    // const int d; err
    // a2 = 9; err
    return 0;
}
